package dominion.player;

import dominion.CardTypes;
import dominion.Dominion;
import dominion.Tresure;
import dominion.kingdom.Market;
import dominion.kingdom.Smithy;
import dominion.kingdom.Village;

public class VillageSmithyPlayer extends TresureOnlyPlayer {

	int village;
	int smithy;
	int market;

	public VillageSmithyPlayer(Dominion dominion) {
		this("VillageSmithy", dominion);
	}

	public VillageSmithyPlayer(String name, Dominion dominion) {
		super(name, dominion);
	}

	@Override
	public int buy(Dominion dominion, int value) {
		if (market < 2 && 4 < value && dominion.buy(this, new Market())) {
			market++;
			value -= 5;
		} else if (smithy < 2 && buySmithy(dominion, value)) {
			smithy++;
			value -= 4;
		} else if (village < 2 && buyVillage(dominion, value)) {
			village++;
			value -= 3;
		} else if ((count(CardTypes.Silver) + count(CardTypes.Gold) < 3)
				&& 2 < value && dominion.buy(this, Tresure.SILVER)) {
			value -= 3;
		} else if (smithy < 3 && buySmithy(dominion, value)) {
			smithy++;
			value -= 4;
		} else if (village < 3 && buyVillage(dominion, value)) {
			village++;
			value -= 3;
		} else {
			value = super.buy(dominion, value);
		}
		return value;
	}

	private boolean buySmithy(Dominion dominion, int value) {
		return 3 < value && dominion.buy(this, new Smithy());
	}

	private boolean buyVillage(Dominion dominion, int value) {
		return 2 < value && dominion.buy(this, new Village());
	}

}
